package com.baitiaojun.video.api.impl;

import com.baitiaojun.api.annotation.UserAuthoritiesAccess;
import com.baitiaojun.api.domain.video.VideoBinaryPicture;
import com.baitiaojun.api.exception.ConditionException;
import com.baitiaojun.api.util.AESUtil;
import com.baitiaojun.api.util.ConstantUtil;
import com.baitiaojun.api.util.FileUtil;
import com.baitiaojun.api.vo.FileInfo;
import com.baitiaojun.api.vo.JsonRes;
import com.baitiaojun.video.api.FileApi;
import com.baitiaojun.video.handler.BulletScreenMaskHandler;
import com.baitiaojun.video.handler.FrameGrabHandler;
import com.baitiaojun.video.service.FileService;
import com.baitiaojun.video.support.FastDFSSupport;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @类名 FileApiImpl
 * @描述 TODO
 * @作者 白条君
 * @创建日期 2022/12/14 17:08
 * @版本 1.0
 */
@RestController
public class FileApiImpl implements FileApi {

    @Autowired
    private FastDFSSupport fastDFSSupport;

    @Autowired
    private FileService fileService;

    @Autowired
    private FrameGrabHandler frameGrabHandler;

    @Autowired
    private BulletScreenMaskHandler bulletScreenMaskHandler;

    @Autowired
    private HttpServletRequest request;

    @UserAuthoritiesAccess(
            accessRole = {ConstantUtil.ROLE_LV0
                    ,ConstantUtil.ROLE_LV1
                    ,ConstantUtil.ROLE_LV2
                    ,ConstantUtil.ROLE_LV3
                    ,ConstantUtil.ROLE_LV4
                    ,ConstantUtil.ROLE_LV5
                    ,ConstantUtil.ROLE_LV6
            })
    @Override
    public JsonRes getFileMd5(MultipartFile file) {
        String fileMd5 = fileService.getFileMd5(file);
        return JsonRes.builder().data(fileMd5);
    }

    @Override
    public JsonRes getFileSlice(MultipartFile file, String md5) throws IOException, ConditionException {
        fastDFSSupport.fileConvertSlice(file, md5);
        return JsonRes.builder().success();
    }

    @UserAuthoritiesAccess(
            accessRole = {ConstantUtil.ROLE_LV0
                    ,ConstantUtil.ROLE_LV1
                    ,ConstantUtil.ROLE_LV2
                    ,ConstantUtil.ROLE_LV3
                    ,ConstantUtil.ROLE_LV4
                    ,ConstantUtil.ROLE_LV5
                    ,ConstantUtil.ROLE_LV6
            })
    @Override
    public JsonRes fastUploadFile(MultipartFile file, String md5, String sliceNo, String totalSize) throws Exception {
        if (ObjectUtils.isEmpty(file) || ObjectUtils.isEmpty(md5) || ObjectUtils.isEmpty(sliceNo) || ObjectUtils.isEmpty(totalSize)) {
            throw new ConditionException("参数异常");
        }
        FileInfo processInfo = fileService.fastUploadFile(file, ConstantUtil.GROUP1, md5, Integer.valueOf(sliceNo), Integer.valueOf(totalSize));
        return JsonRes.builder().success().data(processInfo);
    }

    @Override
    public void getPartialContent(HttpServletResponse response, String filePath) throws Exception {
        fastDFSSupport.getPartialContent(request, response, filePath);
    }

    @Override
    public void getVideoThumbnail(String filePath) throws Exception {
        frameGrabHandler.getVideoFrame(filePath);
    }

    @UserAuthoritiesAccess(
            accessRole = {ConstantUtil.ROLE_LV0
                    ,ConstantUtil.ROLE_LV1
                    ,ConstantUtil.ROLE_LV2
                    ,ConstantUtil.ROLE_LV3
                    ,ConstantUtil.ROLE_LV4
                    ,ConstantUtil.ROLE_LV5
                    ,ConstantUtil.ROLE_LV6
            })
    @Override
    public JsonRes uploadThumbnail(MultipartFile file) throws Exception {
        String filePath = fastDFSSupport.uploadCommonFile(file.getInputStream(), ConstantUtil.GROUP1, FileUtil.getType(file));
        AESUtil aesUtil = AESUtil.lazyGetAesUtil();
        return JsonRes.builder().success().data(aesUtil.encrypt(filePath));
    }

    /**
     * 描述：截取视频帧图，对帧图进行人像分割，人像分割图转为黑白剪影图，返回前端
     * @param videoId
     * @param md5
     * @return
     * @throws Exception
     */
    @UserAuthoritiesAccess(
            accessRole = {ConstantUtil.ROLE_LV0
                    ,ConstantUtil.ROLE_LV1
                    ,ConstantUtil.ROLE_LV2
                    ,ConstantUtil.ROLE_LV3
                    ,ConstantUtil.ROLE_LV4
                    ,ConstantUtil.ROLE_LV5
                    ,ConstantUtil.ROLE_LV6
            })
    @Override
    public JsonRes genericBulletScreenMask(Long videoId, String md5) throws Exception {
        bulletScreenMaskHandler.videoConvertSilhouette(videoId, md5);
        return JsonRes.builder().success();
    }

    @Override
    public JsonRes getBulletScreenMask(Long videoId, String url, HttpServletResponse response) throws Exception {
        AESUtil aesUtil = new AESUtil(ConstantUtil.AES_SEED);
        Map<String, String> filePathMap = FileUtil.splitFilePath(aesUtil.decrypt(url.replace(" ", "+")));
        String group = filePathMap.get("group");
        String filePath = filePathMap.get("filePath");
        fastDFSSupport.getThumbnail(group, request, response, filePath);
        return JsonRes.builder().success();
    }
}